const initState = {
    menus:[],
    count:1
}

// 定义一个纯函数reducer，专门用来操作state中的数据,要返回一个新的state
const appReducer = (state = initState, action) => {
    switch (action.type) {
        case 'SET_MENUS':
            const list = action.payload.filter(v=>v.path==='/')[0].children.map((item,index)=>{
                if(item.children){
                    item.newArr = item.children.map(item2=>(getItem(item2.title,item2.path)))
                }
                if(item.path){
                    return {
                        ...getItem(item.title,item.path,null,item.newArr)
                    }
                }
                return{
                }
            })
            // return { ...state,menus:action.payload}
            return { ...state,menus:list}
        case "SET_COUNT":
            return {...state,count:action.payload}
        default:
            return state
    }
}

const getItem = (label, key, icon, children, type) => {
    return{
        key,
        icon,
        children,
        label,
        type
    }
}

// 导出
export default appReducer
